package cn.wolfcode.cloud.shop.service.impl;

import cn.wolfcode.cloud.shop.domain.SeckillOrder;
import cn.wolfcode.cloud.shop.mapper.SeckillOrderMapper;
import cn.wolfcode.cloud.shop.redis.SeckillRedisKey;
import cn.wolfcode.cloud.shop.service.ISeckillOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

/**
 * @author Administrator
 */
@Service
public class SeckillOrderServiceImpl implements ISeckillOrderService {

    @Autowired
    private SeckillOrderMapper seckillOrderMapper;

    @Autowired
    private StringRedisTemplate redisTemplate;

    @Override
    public SeckillOrder queryByUserIdAndSeckillId(Long userId, Long seckillId) {

        return seckillOrderMapper.selectByUserIdAndSeckill(userId,seckillId);
    }

    @Override
    public void createSeckillOrder(String orderNo, Long id, Long userId) {
        seckillOrderMapper.insertOrder(orderNo,id,userId);

        //将当前秒杀成功的用户,保存到redis中
        redisTemplate.opsForSet().add(SeckillRedisKey.SECKILL_ORDERS_SET.getRealKey(id+""),userId+"");
    }

}
